$(function(){
	
	var vm = new Vue({
		el:'#Main',
		data:{
			type:0,
			userData:data.fans,
			userId:0,
		},
		methods:{
			//操作
			addMask:function(){
				var that = this;
						
				//获取当前数据对象
				var target;
				for(i in that.userData){
					if(that.userData[i].id == that.userId){
						target = that.userData[i];
					};
				};
				//传值给加标签函数
				addTag(target.tag,function(arr){
					arr.length > 0 ? target.tag = arr : target.tag = null;
				});
				
			},
		},
		created:function(){
			var that = this;
			
			//获取当前用户id
			var userId = getUrlParam('userId');
			that.userId = userId;
			
		},
		mounted:function(){
			new copy('.history-1',10).data();
			new copy('.history-2',10).data();
			imgRate();
			pullSwiper('#Main',{
				up:true,//启动下拉刷新,也可自定义函数，默认禁用
				down:true,//启用上拉加载,也可自定义函数，默认禁用
			});
			pullSwiper('#addTag');
			pullSwiper('#infoEditMod');
		},
	});
	
	
	//删除标签
	$("#addTag .tag-wrap").delegate('a','click',function(){
		var target = $(this);
		dmAlert('确定要删除该标签',{
			success:function(){
				target.remove();
			},
		});
	});
	
	//新增标签
	$("#addBtn").click(function(){
		var val = $("#tag").val();
		var isAdd = true;
		$("#addTag .tag-wrap").find('.tag').each(function(){
			var tag = $(this).text();
			if(tag == val){
				isAdd = false;
			};
		});
		if(val != ''){
			if(isAdd){
				$('#addTag .tag-wrap').append(`<a><span class='tag'>${val}</span><span>×</span></a>`);
				$("#tag").val('');
			}else{
				tip('标签重复了');
			};
		};
	});
	
});

function refresh(pull){
	pull.innerHTML = '刷新完成';
};

function reload(pull){
	pull.innerHTML = '已加载全部数据';
};


//添加标签
function addTag(arr,callback){
	var tagHTML = '';
	
	for(i in arr){
		tagHTML += `<a><span class='tag'>${arr[i]}</span><span>×</span></a>`;
	};
	$('#addTag .tag-wrap').html(tagHTML);
	
	openSubMain('#addTagMask');
	
	//完成
	$("#achievedBtn").one('click',function(){
		var tagArr = [];
		$('#addTag .tag-wrap').find('.tag').each(function(){
			tagArr.push($(this).text());
		});
		
		if(typeof callback == 'function'){
			callback(tagArr);
		};
		closeSubMain($("#addTagMask"));
	});
	
};


//切换
function historyTab(obj){
	$(obj).addClass('on').siblings().removeClass('on');
	var index = $(obj).index();
	$(".history-box").hide().eq(index).show();
	imgRate();
};


//打开意向
function editIntent(){
	openSubMain('#indentMask');
	//如果没有地址，提示去添加地址
	setTimeout(function(){
		dmAlert('暂无地址，无法提交',{
			type:0,
			enter:'完善地址',
			success:function(){
				location.href = 'user_editInfo.html';
			},
		});
	},500);
};


//信息编辑
function editInfo(){
	var id = getUrlParam('userId');
	location.href='user_editInfo.html?userId='+id;
};
